﻿<template>
  <a-card :bordered="false">
    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="hanldleAdd()">新建</a-button>
      <!-- <a-button
        type="primary"
        icon="minus"
        @click="handleDelete(selectedRowKeys)"
        :disabled="!hasSelected()"
        :loading="loading"
        >删除</a-button
      > -->
      <a-button type="primary" icon="redo" @click="getDataList()">刷新</a-button>
    </div>

    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="4" :sm="24">
            <a-form-item label="查询类别">
              <a-select allowClear v-model="queryParam.condition">
                <a-select-option key="RequestDepartmentName">服务请求部门</a-select-option>
                <a-select-option key="DeliveryDepartmentName">提供服务部门</a-select-option>
                <a-select-option key="Remark">服务请求清单</a-select-option>
                <a-select-option key="AccountName">科目</a-select-option>
                <a-select-option key="ApprovalName">审批人</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="24">
            <a-form-item>
              <a-input v-model="queryParam.keyword" placeholder="关键字" />
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="24">
            <a-button
              type="primary"
              @click="
                () => {
                  this.pagination.current = 1
                  this.getDataList()
                }
              "
              >查询</a-button
            >
            <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <a-table
      ref="table"
      :columns="columns"
      :rowKey="(row) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :bordered="true"
      size="small"
    >
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)" v-if="record.State == 0">编辑</a>
          <a-divider type="vertical" v-if="record.State == 0" />
          <a @click="handleDelete([record.Id])" v-if="record.State == 0">删除</a>
          <a-divider type="vertical" v-if="record.State == 0" />
          <a @click="handleSubmit(record)" v-if="record.State == 0">提交</a>
          <a-divider type="vertical" v-if="record.State > 0" />
          <a @click="handleShow(record)" v-if="record.State > 0">查看</a>
          <!-- <a @click="handleSubmit(record)" v-if="record.State == 1">审批</a>
          <a @click="handleSubmit(record)" v-if="record.State == 2">完工确认</a>
          <a @click="handleSubmit(record)" v-if="record.State == 3">评价</a> -->
          <a-divider type="vertical" v-if="record.State != 0" />
          <a @click="approvalHistory(record.Id)" v-if="record.State != 0">审批记录</a>
        </template>
      </span>
    </a-table>

    <edit-form ref="editForm" :parentObj="this"></edit-form>
    <approval-history ref="approvalHistory" :parentObj="this"></approval-history>
    <approval ref="approval" :parentObj="this"></approval>
  </a-card>
</template>

<script>
import EditForm from './EditForm'
import ApprovalHistory from './ApprovalHistory'
import Approval from './Approval'

const columns = [
  { title: '服务请求部门', dataIndex: 'RequestDepartmentName', width: '10%' },
  { title: '提供服务部门', dataIndex: 'DeliveryDepartmentName', width: '10%' },
  { title: '服务请求清单(备注)', dataIndex: 'Remark', width: '12%' },
  { title: '金额', dataIndex: 'Amount', width: '10%' },
  { title: '科目', dataIndex: 'AccountName', width: '10%' },
  { title: '状态', dataIndex: 'StateName', width: '10%' },
  { title: '申请时间', dataIndex: 'CreateTime', width: '10%' },
  { title: '审批人', dataIndex: 'ApprovalName', width: '10%' },
  { title: '操作', dataIndex: 'action', scopedSlots: { customRender: 'action' } },
]

export default {
  components: {
    EditForm,
    ApprovalHistory,
    Approval,
  },
  mounted() {
    this.getDataList()
  },
  data() {
    return {
      data: [],
      pagination: {
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
      },
      filters: {},
      sorter: { field: 'State', order: 'asc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: [],
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.sorter = { ...sorter }
      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []
      let userInfo = JSON.parse(localStorage.getItem('userInfo'))
      this.queryParam.UserId = userInfo.Id
      this.queryParam.PT = 'C'
      console.log(this.sorter.field)
      this.loading = true
      this.$http
        .post('/GBT_Manage/GBT_ServiceRequest/GetDataList', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'State',
          SortType: this.sorter.order,
          Search: this.queryParam,
          ...this.filters,
        })
        .then((resJson) => {
          console.log('请求单据：', resJson)

          this.loading = false
          if (resJson.Success) {
            let list = resJson.Data
            list.forEach((element) => {
              switch (element.State) {
                case 0:
                  element.StateName = '发起'
                  break
                case 1:
                  element.StateName = '审批'
                  break
                case 2:
                  element.StateName = '确认完工'
                  break
                case 3:
                  element.StateName = '评价'
                  break
                case 4:
                  element.StateName = '完结'
                  break
              }
            })
            this.data = list

            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          } else {
            this.$message.error(resJson.Msg)
          }
        })
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    hanldleAdd() {
      let param = { enable: true }
      this.$refs.editForm.openForm(param)
    },
    handleEdit(item) {
      let param = { item: item, enable: true }
      this.$refs.editForm.openForm(param)
    },
    handleShow(item) {
      let param = { item: item, enable: false }
      this.$refs.editForm.openForm(param)
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/GBT_Manage/GBT_ServiceRequest/DeleteData', ids).then((resJson) => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        },
      })
    },
    approal(param) {
      var thisObj = this
      thisObj.loading = true
      this.$http.post('/GBT_Manage/GBT_ServiceRequest/Approval', param).then((resJson) => {
        thisObj.loading = false
        if (resJson.Success) {
          thisObj.$message.success('操作成功!')
        } else {
          thisObj.$message.error(resJson.Msg)
        }
        thisObj.getDataList()
      })
    },
    handleSubmit(item) {
      console.log('审批：', item)
      this.$refs.approval.openForm(item)
    },
    approvalHistory(id) {
      this.$refs.approvalHistory.openForm(id)
    },
  },
}
</script>
